﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;

namespace G.Controls
{
    public class ControlService
    {
        #region 控件显示或隐藏，主要是配合绑定来做
        /// <summary>
        /// 通过绑定某个内容来判断是否显示或者隐藏控件，如果内容为空那么显示否则隐藏，内容如果是字符串，那么也可以有类似的判断， 主要是用来做类似watemarket效果
        /// </summary>
        public static readonly DependencyProperty IsNullToVisibleProperty = DependencyProperty.RegisterAttached("IsNullToVisible", typeof(object), typeof(ControlService), new PropertyMetadata(null, OnIsNullToVisibleChanged));


        private static void OnIsNullToVisibleChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            UIElement u = o as UIElement;
            object obj = ControlService.GetIsNullToVisible(u);
            if (obj == null)
            {
                u.Visibility = Visibility.Visible;
            }
            else
            {
                if (obj is string)
                {
                    if (string.IsNullOrEmpty(obj as string))
                    {
                        u.Visibility = Visibility.Visible;
                    }else
                        u.Visibility = Visibility.Collapsed;
                }
                else
                    u.Visibility = Visibility.Collapsed;
            }
        }

        public static object GetIsNullToVisible(UIElement element)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }

            return element.GetValue(IsNullToVisibleProperty);
        }

        public static void SetIsNullToVisible(UIElement element, object value)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }
            element.SetValue(IsNullToVisibleProperty, value);
        }
        #endregion
    }
}
